/******************************************************************************
 * Copyright (C) 671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef NODECPP_FOUNDATION_THREAD_POSIX_H_
#define NODECPP_FOUNDATION_THREAD_POSIX_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <pthread.h>

namespace NodeCpp
{
    class Runnable;

    class ThreadImpl
    {
    public:
        /* Thread priority */
        enum Priority
        {
            PRIO_LOWEST_IMPL = 0,
            PRIO_LOW_IMPL = 1,
            PRIO_NORMAL_IMPL = 2,
            PRIO_HIGH_IMPL = 3,
            PRIO_HIGHEST_IMPL = 4
        };
        /* Thread ID */
        typedef pthread_t   ID;

        ThreadImpl(void);
        ~ThreadImpl(void);

        bool StartImpl(Runnable*);
        void JoinImpl(void);
        bool JoinableImpl(void);
        bool SetPriorityImpl(int);
        Priority GetPriorityImpl(void) const { return Priority_; }
        ID GetIDImpl(void) const { return ID_; }

    private:
        static void* _Proc(void*);

        ID ID_;
        Runnable* Runnable_;
        Priority Priority_;
    };

    class ThisThreadImpl
        {
        protected:
            ThisThreadImpl(void){}
            ~ThisThreadImpl(void){}

        public:
            static ThreadImpl::ID GetIDImpl(void);
            static void SleepImpl(long);
        };
}

#endif // NODECPP_FOUNDATION_THREAD_POSIX_H_
